package com.desmond.squarecamera;

import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;

public class ResizeAnimation extends AnimatorValue {

    final int mStartLength;
    final int mFinalLength;
    final boolean mIsPortrait;
    final Component mView;

    public ResizeAnimation(Component view, final ImageParameters imageParameters) {
        mIsPortrait = imageParameters.isPortrait();
        mView = view;
        mStartLength = mIsPortrait ? mView.getHeight() : mView.getWidth();
        mFinalLength = imageParameters.getAnimationParameter();
    }

    public void initUpdate() {
        setValueUpdateListener(new ValueUpdateListener() {
            @Override
            public void onUpdate(AnimatorValue animatorValue, float v) {
                int newLength = (int) (mStartLength + (mFinalLength - mStartLength) * v);

//                if (mIsPortrait) {
//                    mView.getLayoutConfig().height = newLength;
//                    mView.setHeight(newLength);
//                } else {
//                    mView.setWidth(newLength);
//                }

                ComponentContainer.LayoutConfig layoutConfig = mView.getLayoutConfig();
                layoutConfig.height = newLength;
                mView.setLayoutConfig(layoutConfig);

                mView.postLayout();
            }
        });

        setStateChangedListener(new StateChangedListener() {
            @Override
            public void onStart(Animator animator) {

            }

            @Override
            public void onStop(Animator animator) {

            }

            @Override
            public void onCancel(Animator animator) {

            }

            @Override
            public void onEnd(Animator animator) {
                ComponentContainer.LayoutConfig layoutConfig = mView.getLayoutConfig();
                if (layoutConfig.height > 0) {
                    layoutConfig.height = 0;
                    mView.setLayoutConfig(layoutConfig);
                    mView.postLayout();
                }
            }

            @Override
            public void onPause(Animator animator) {

            }

            @Override
            public void onResume(Animator animator) {

            }
        });
    }
}
